home *** CD-ROM | disk | FTP | other *** search
/ Trading on the Edge / Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin / pc / mac_file / vendor_d / neuralwa / nw2v50 / bsbwh.c < prev    next >
Text File  |  1993-08-23  |  29KB  |  935 lines

  1. /* 15:40 04-Jul-88  (bsbwh.c)  Brain-State-in-a-Box, Widrow-Hoff learning */
  2.  
  3. /************************************************************************
  4.  * Copyright(C) 1987-1992 NeuralWare Inc                                *
  5.  * Penn Center West, IV-227, Pittsburgh, PA 15276                       *
  6.  * Telephone: (412) 787-8222    FAX: (412) 787-8220                     *
  7.  *                                                                      *
  8.  * All rights reserved.  No part of this program may be reproduced,     *
  9.  * stored in a retrieval system, or transmitted, in any form or by any  *
  10.  * means, electronic, mechanical, photocopying, recording or otherwise  *
  11.  * without the prior written permission of the copyright owner,         *
  12.  * NeuralWare, Inc.                                                     *
  13.  *                                                                      *
  14.  *                          PROPRIETARY NOTICE                          *
  15.  *                                                                      *
  16.  * This document is the property of NeuralWare, Inc. and contains       *
  17.  * trade-secrets and other proprietary information.  The information    *
  18.  * herein is reserved as proprietary to NeuralWare, and is not to be    *
  19.  * published, reproduced, copied, disclosed, used, or reverse           *
  20.  * engineered without the express written consent of a duly authorized  *
  21.  * representative of NeuralWare.                                        *
  22.  ************************************************************************
  23.  */
  24.  
  25.  
  26. #include "userutl.h"
  27. #include <string.h>
  28. #ifndef SUN
  29. #ifndef DLC
  30. #include <stdlib.h>
  31. #endif
  32. #endif
  33.  
  34. #ifdef MAC
  35. #include "macuio.redef"
  36. #endif
  37. /************************************************************************
  38.  *                  *
  39.  *  User I/O for Brain-State-in-a-Box Widrow-Hoff Example   *
  40.  *                  *
  41.  ************************************************************************
  42.     The data examples contained in this module can be written to a
  43.     standard ".nna" Ascii File by "EXECUTE NETWORK/ATTENTION".
  44.  */
  45.  
  46. #define ORGANISM  0
  47. #define TYPE_ORG  1
  48. #define DISEASE   2
  49. #define DRUG1   3
  50. #define SIDE_EFF  4
  51. #define M_ADMIN   5
  52. #define DRUG2   6
  53.  
  54. #define N_CATEG   7 /* Number of categories */
  55.  
  56. #define N_ORG   16
  57. #define N_TYO   7
  58. #define N_DIS   15
  59. #define N_SID   5
  60. #define N_ADM   3
  61. #define N_DRG   8
  62.  
  63. #define NULL_STR    0
  64.  
  65. /* Indices into arrays */
  66. static  int  curr_index[N_CATEG] = {0};
  67.  
  68. static  int  num_words[N_CATEG] =
  69.        { N_ORG, N_TYO, N_DIS, N_DRG, N_SID, N_ADM, N_DRG };
  70. static  int  pos_abbrev[N_CATEG] = { 0, 8, 12, 18, 4, 13, 18 };
  71.  
  72. static  char  **c_cat_array[N_CATEG] = {0};
  73. static  char  **a_cat_array[N_CATEG] = {0};
  74.  
  75. static  short  curr_mode = 0;
  76.  
  77. static char *c_organism[] = { /* Complete words for organisms */
  78.  NULL_STR, NULL_STR, NULL_STR, NULL_STR,
  79.  NULL_STR, NULL_STR, NULL_STR, NULL_STR,
  80.  NULL_STR, NULL_STR, NULL_STR, NULL_STR,
  81.  NULL_STR, NULL_STR, NULL_STR, NULL_STR
  82. };
  83. static char *a_organism[] = { /* Abbreviated words for organisms */
  84.  NULL_STR, NULL_STR, NULL_STR, NULL_STR,
  85.  NULL_STR, NULL_STR, NULL_STR, NULL_STR,
  86.  NULL_STR, NULL_STR, NULL_STR, NULL_STR,
  87.  NULL_STR, NULL_STR, NULL_STR, NULL_STR
  88. };
  89. static char *c_type_org[] = { /* Complete words for organism types */
  90.  NULL_STR, NULL_STR, NULL_STR,
  91.  NULL_STR, NULL_STR, NULL_STR,
  92.  NULL_STR
  93. };
  94. static  char    *a_type_org[] = { /* Abbrev words for organism types */
  95.  NULL_STR, NULL_STR, NULL_STR,
  96.  NULL_STR, NULL_STR, NULL_STR,
  97.  NULL_STR
  98. };
  99. static  char    *c_disease[] = { /* Complete words for diseases */
  100.  NULL_STR, NULL_STR, NULL_STR, NULL_STR,
  101.  NULL_STR, NULL_STR, NULL_STR, NULL_STR,
  102.  NULL_STR, NULL_STR, NULL_STR, NULL_STR,
  103.  NULL_STR, NULL_STR, NULL_STR
  104. };
  105. static  char    *a_disease[] = { /* Abbreviated words for diseases */
  106.  NULL_STR, NULL_STR, NULL_STR, NULL_STR,
  107.  NULL_STR, NULL_STR, NULL_STR, NULL_STR,
  108.  NULL_STR, NULL_STR, NULL_STR, NULL_STR,
  109.  NULL_STR, NULL_STR, NULL_STR
  110. };
  111. static  char    *c_side_eff[] = { /* Side effects */
  112.  NULL_STR, NULL_STR, NULL_STR,
  113.  NULL_STR, NULL_STR
  114. };
  115. static  char    *a_side_eff[] = { /* Side effects ( abbrev ) */
  116.  NULL_STR, NULL_STR, NULL_STR,
  117.  NULL_STR, NULL_STR
  118. };
  119. static  char    *c_m_admin[] = { /* Methods of administration */
  120.  NULL_STR, NULL_STR, NULL_STR
  121. };
  122. static  char    *a_m_admin[] = { /* Meth admin */
  123.  NULL_STR, NULL_STR, NULL_STR
  124. };
  125. static  char    *c_drug[] = { /* Drugs */
  126.  NULL_STR, NULL_STR, NULL_STR,
  127.  NULL_STR, NULL_STR, NULL_STR,
  128.  NULL_STR, NULL_STR
  129. };
  130. static  char    *a_drug[] = { /* Drgs */
  131.  NULL_STR, NULL_STR, NULL_STR,
  132.  NULL_STR, NULL_STR, NULL_STR,
  133.  NULL_STR, NULL_STR
  134. };
  135.  
  136. /* Local menu definitions */
  137.  
  138. static  char    *mode_text[] = { /* menu text for mode option */
  139.  NULL_STR, NULL_STR
  140. };
  141.  
  142. #define M_MODE    0
  143. #define M_CLEAR   1
  144. #define M_ENTER   2
  145. #define M_QUIT    3
  146.  
  147. #define M_INPUT   0
  148.  
  149. #define M_OUTPUT  0
  150.  
  151. static GMENU_ITEM Cat1List[] = { /* Menu List */
  152.     { ORGANISM, NULL_STR, NULL_STR },
  153.     { TYPE_ORG, NULL_STR, NULL_STR },
  154.     { DISEASE,  NULL_STR, NULL_STR },
  155.     { DRUG1,     NULL_STR, NULL_STR }
  156. };
  157.  
  158. static GMENU_ITEM Cat2List[] = { /* Menu List */
  159.     { SIDE_EFF, NULL_STR, NULL_STR  },
  160.     { M_ADMIN,  NULL_STR, NULL_STR },
  161.     { DRUG2,      NULL_STR, NULL_STR }
  162. };
  163. static GMENU_ITEM UtList[] = { /* Menu List */
  164.       { M_MODE,     NULL_STR, NULL_STR },
  165.       { M_CLEAR,    NULL_STR, NULL_STR },
  166.       { M_ENTER,    NULL_STR, NULL_STR },
  167.       { M_QUIT,     NULL_STR, NULL_STR }
  168. };
  169. static GMENU_ITEM InItem = 
  170.     { M_INPUT, NULL_STR, NULL_STR , 0x0002 };
  171. static GMENU_ITEM OutItem = 
  172.     { M_OUTPUT, NULL_STR, NULL_STR , 0x0002 };
  173.  
  174. static GMENU  CatMenu[2]  = { /* Category menus */
  175.   { 0,    /* Organism mode */
  176.   4,        /* 4 items */
  177.   1       /* key */
  178.   },
  179.   { 0,    /* Side effect mode */
  180.   3,        /* 3 items */
  181.   1       /* key */
  182.   }
  183. };
  184.  
  185. static GMENU  UtMenu = {  /* Utility menu */
  186.   0,
  187.   4,        /* 4 items */
  188.   2       /* key */
  189. };
  190.  
  191. static GMENU  InpMenu = { /* Displays abbreviated input vector */
  192.   0,
  193.   1,        /* 1 item */
  194.   3       /* key */
  195. };
  196.  
  197. static GMENU  OutMenu = { /* Displays abbreviated ouput vector */
  198.   0,
  199.   1,        /* 1 item */
  200.   4       /* key */
  201. };
  202.  
  203. /* menu window positions */
  204. static int  x_cat,y_cat,x_cat1,x_ut,y_ut,x_inp,y_inp,x_out,y_out;
  205.  
  206. #ifdef PROTOTYPING
  207. /* --- prototypes --- */
  208. void    abbreviate( int );
  209. void    encode( int, float * );
  210. char  decode( float *, double );
  211. #endif
  212.  
  213. void abbreviate( item ) /* abbreviate Input vector */
  214. int item; /* item to abbreviate ( all = -1 ) */
  215. {
  216.   if ( item == -1 ) {
  217.     if ( curr_mode == 0 ) {
  218.       strcpy( InItem.text, a_organism[curr_index[ORGANISM]] );
  219.       strcat( InItem.text, a_type_org[curr_index[TYPE_ORG]] );
  220.       strcat( InItem.text, a_disease[curr_index[DISEASE]] );
  221.       strcat( InItem.text, a_drug[curr_index[DRUG1]] );
  222.     } else if ( curr_mode == 1 ) {
  223.       strcpy( InItem.text, "SiEf" );
  224.       strcat( InItem.text, a_side_eff[curr_index[SIDE_EFF]] );
  225.       strcat( InItem.text, a_m_admin[curr_index[M_ADMIN]] );
  226.       strcat( InItem.text, a_drug[curr_index[DRUG2]] );
  227.     }
  228.   } else {
  229.     if ( item == SIDE_EFF ) strncpy( InItem.text, "SiEf", 4 );
  230.     strncpy( &InItem.text[pos_abbrev[item]],
  231.       a_cat_array[item][curr_index[item]],
  232.       strlen( a_cat_array[item][0] ) );
  233.   }
  234. }
  235.  
  236. void encode( a, vp )
  237. int a;  /* character to encode */
  238. float *vp;  /* pointer to vector of floats containing encoded char */
  239. {
  240.   unsigned char   wa;
  241.   unsigned char mask;
  242.   int   wx, parity;
  243.  
  244.   if ( a == '_' ) wa = 0xff;
  245.   else if ( a >= 'a' && a <= 'z' ) wa = a - 'a';
  246.   else if ( a >= 'A' && a <= 'Z' ) wa = a - 'A';
  247.   else if ( a == '+' ) wa = 26;
  248.   else if ( a == '-' ) wa = 27;
  249.   else if ( a == '.' ) wa = 28;
  250.   else if ( a == ' ' ) wa = 29;
  251.   else wa = 30;
  252.  
  253.   /* Find parity */
  254.   parity = 0;
  255.   for ( wx = 0, mask = 0x10; wx < 5; wx++, mask >>= 1 )
  256.     if ( (wa & mask) != 0 ) parity++;
  257.   parity %= 2;
  258.   
  259.   if ( a >= 'A' && a <= 'Z' ) { /* Make capitals odd parity */
  260.     if (parity == 0) wa |= 0x20;
  261.   } else if ( a != '_' ) { /* make rest even parity */
  262.     if ( parity ) wa |= 0x20;
  263.   }
  264.   
  265.   for ( wx = 0, mask = 0x20; wx < 6; wx++, mask >>= 1 ) {
  266.     if ( wa == 0xff ) vp[wx] = 0.0;
  267.     else if ( (wa & mask) != 0 ) vp[wx] = 1.0;
  268.     else vp[wx] = -1.0;
  269.   }
  270. }
  271.   
  272. char decode( vp, thr )  /* Returns character */
  273. float *vp;  /* vector of floats containing encoded char */
  274. double  thr;  /* threshold to determine ON/OFF */
  275. {
  276.   unsigned char wa;
  277.   char    rc;
  278.   unsigned char mask;
  279.   int   wx, parity;
  280.  
  281.   wa = 0;
  282.  
  283.   for ( wx = 0, mask = 0x20; wx < 6; wx++, mask >>= 1 ) {
  284.     if ( vp[wx] >= thr ) wa |= mask;
  285.     else if ( vp[wx] > -thr ) { /* if between thresholds set all to 0 */
  286.       wa = 0xff;
  287.       break;
  288.     }
  289.   }
  290.  
  291.   /* Find parity */
  292.   parity = 0;
  293.   for ( wx = 0, mask = 0x20; wx < 6; wx++, mask >>= 1 )
  294.     if ( (wa & mask) != 0 ) parity++;
  295.   parity %= 2;
  296.   
  297.   if ( wa == 0xff ) rc = '_';
  298.   else if ( parity ) { /* odd parity - check for capitals */
  299.     wa &= 0x001f;
  300.     if ( wa >= 0 && wa < 26 ) rc = wa + 'A';
  301.     else rc = '?';
  302.   } else { /* Even parity - check for rest */
  303.     wa &= 0x001f;
  304.     if ( wa >= 0 && wa < 26 ) rc = wa + 'a';
  305.     else if ( wa == 26 ) rc = '+';
  306.     else if ( wa == 27 ) rc = '-';
  307.     else if ( wa == 28 ) rc = '.';
  308.     else if ( wa == 29 ) rc = ' ';
  309.     else rc = '?';
  310.   }
  311.   
  312.   return( rc );
  313. }
  314.   
  315. /************************************************************************
  316.  *                  *
  317.  *  UsrIO - user I/O routine to handle requests from NWORKS   *
  318.  *                  *
  319.  ************************************************************************
  320.  */
  321.  
  322. static int InitFlag = 0;    /* initialize things flag */
  323.  
  324. void UsrIO()     /* handle NWORKS requests */
  325. {
  326.     GMENU_ITEM  *gmip;
  327.     char *sp;         /* string pointer */
  328.     char  buf[100];       /* character buffer */
  329.     int   wx, wy;       /* work indices */
  330.     int   key, xp, yp, button;      /* mouse parameters */
  331.     int  *ci, nw;
  332.     int   xsize, ysize, ncolor, chrx, chry; /* graphics parameters */
  333.     int   iv_num;       /* input vector number */
  334.     static int   wr_count;      /* count for RQ_WRSTEP */
  335.     float wr[6], *p_outv;
  336.     FILE *asc_fp;
  337.     static char InItemTextBuf[30] = {0};  /* work buffer */
  338.     static char OutItemTextBuf[30] = {0}; /* second work buffer */
  339.  
  340.   if ( InitFlag == 0 ) {
  341.   /* open any files which may be required at this point in the
  342.      code. */
  343.   /* Complete words for organisms */
  344.   c_organism[0] = "               ";
  345.   c_organism[1] = "Staphylococcus ";
  346.   c_organism[2] = "Streptococcus  ";
  347.   c_organism[3] = "Neisseria      ";
  348.   c_organism[4] = "Corynebacterium";
  349.   c_organism[5] = "Clostridium        ";
  350.   c_organism[6] = "Enterobacter   ";
  351.   c_organism[7] = "Salmonella         ";
  352.   c_organism[8] = "Treponema      ";
  353.   c_organism[9] = "Candida            ";
  354.   c_organism[10] = "Histoplasma    ";
  355.   c_organism[11] = "Escherichia        ";
  356.   c_organism[12] = "Proteus        ";
  357.   c_organism[13] = "Yersinia           ";
  358.   c_organism[14] = "Cryptococcus   ";
  359.   c_organism[15] = "Aspergillus        ";
  360.  
  361.   /* Abbreviated words for organisms */
  362.   a_organism[0] = "________";
  363.   a_organism[1] = "Staphaur";
  364.   a_organism[2] = "Streptop";
  365.   a_organism[3] = "Neisseri";
  366.   a_organism[4] = "Coryneba";
  367.   a_organism[5] = "Clostrid";
  368.   a_organism[6] = "Enteroba";
  369.   a_organism[7] = "Salmonel";
  370.   a_organism[8] = "Treponem";
  371.   a_organism[9] = "Candidaa";
  372.   a_organism[10] = "Histopla";
  373.   a_organism[11] = "E. Coli ";
  374.   a_organism[12] = "Proteus ";
  375.   a_organism[13] = "Yersinap";
  376.   a_organism[14] = "Cryptoco";
  377.   a_organism[15] = "Aspergil";
  378.  
  379.   /* Complete words for organism types */
  380.   c_type_org[0] = "                 ";
  381.   c_type_org[1] = "Gram +ve coccus  ";
  382.   c_type_org[2] = "Gram -ve coccus      ";
  383.   c_type_org[3] = "Gram +ve bacillus";
  384.   c_type_org[4] = "Gram -ve bacillus";
  385.   c_type_org[5] = "Spirochetal          ";
  386.   c_type_org[6] = "Fungal           " ;
  387.  
  388.   /* Abbrev words for organism types */
  389.   a_type_org[0] = "____";
  390.   a_type_org[1] = "+coc";
  391.   a_type_org[2] = "-coc";
  392.   a_type_org[3] = "+bac";
  393.   a_type_org[4] = "-bac";
  394.   a_type_org[5] = "spir";
  395.   a_type_org[6] = "fung";
  396.     
  397.   /* Complete words for diseases */
  398.   c_disease[0] = "                 ";
  399.   c_disease[1] = "Endocarditis ";
  400.   c_disease[2] = "Pneumonia        ";
  401.   c_disease[3] = "Gonorrhea    ";
  402.   c_disease[4] = "Tetanus          ";
  403.   c_disease[5] = "Urinary Tract";
  404.   c_disease[6] = "Typhoid  ";
  405.   c_disease[7] = "Syphilis     ";
  406.   c_disease[8] = "Lesion           ";
  407.   c_disease[9] = "Meningitis       ";
  408.   c_disease[10] = "Scarlet Fever";
  409.   c_disease[11] = "Pharyngitis  ";
  410.   c_disease[12] = "Gangrene         ";
  411.   c_disease[13] = "Plague   ";
  412.   c_disease[14] = "Yaws         ";
  413.  
  414.   /* Abbreviated words for diseases */
  415.   a_disease[0] = "______";
  416.   a_disease[1] = "Endoca";
  417.   a_disease[2] = "Pneumo";
  418.   a_disease[3] = "Gonorh";
  419.   a_disease[4] = "Tetanu";
  420.   a_disease[5] = "UrTrIn";
  421.   a_disease[6] = "Typhoi";
  422.   a_disease[7] = "Syphil";
  423.   a_disease[8] = "Lesion";
  424.   a_disease[9] = "Mening";
  425.   a_disease[10] = "ScarFe";
  426.   a_disease[11] = "Pharyn";
  427.   a_disease[12] = "Gangre";
  428.   a_disease[13] = "Plague";
  429.   a_disease[14] = "Yaws      ";
  430.  
  431.   /* Side effects */ 
  432.   c_side_eff[0] = "                ";
  433.   c_side_eff[1] = "Hypersensitivity";
  434.   c_side_eff[2] = "Aplastic Anemia ";
  435.   c_side_eff[3] = "Ototoxicity     ";
  436.   c_side_eff[4] = "Kidneys             ";
  437.  
  438.     /* Side effects ( abbrev ) */
  439.     a_side_eff[0] = "_________";
  440.     a_side_eff[1] = "Hypersens";
  441.     a_side_eff[2] = "AplasticA";
  442.   a_side_eff[3] = "Ototoxic ";
  443.   a_side_eff[4] = "Kidneys++";
  444.  
  445.   /* Methods of administration */
  446.   c_m_admin[0] = "         ";
  447.   c_m_admin[1] = "Injection";
  448.   c_m_admin[2] = "Oral         ";
  449.  
  450.   /* Meth admin */
  451.   a_m_admin[0] = "_____";
  452.   a_m_admin[1] = "Inje ";
  453.   a_m_admin[2] = "Oral ";
  454.    
  455.   /* Drugs */
  456.   c_drug[0] = "               ";
  457.   c_drug[1] = "Penicillin         ";
  458.   c_drug[2] = "Ampicillin     ";
  459.   c_drug[3] = "Cephalosporin  ";
  460.   c_drug[4] = "Chloramphenicol";
  461.   c_drug[5] = "Amphotericin   ";
  462.   c_drug[6] = "Gentamycin     ";
  463.   c_drug[7] = "Tetracyclin        ";
  464.   
  465.   /* Drugs (abbrev)*/
  466.   a_drug[0] = "_______";
  467.   a_drug[1] = "Penicil";
  468.   a_drug[2] = "Ampicil";
  469.   a_drug[3] = "Cephalo";
  470.   a_drug[4] = "Chloram";
  471.   a_drug[5] = "Amphote";
  472.   a_drug[6] = "Gentamy";
  473.   a_drug[7] = "Tetracy";
  474.  
  475.   /* menu text for mode option */
  476.   mode_text[0] = "Mode: Disease         ";
  477.   mode_text[1] = "Mode: Side Effects";
  478.  
  479.   /* Menu List */
  480.   UtList[0].text = "Mode: Disease     ";
  481.   UtList[1].text = "Clear";
  482.   UtList[2].text = "Enter";
  483.   UtList[3].text = "Quit";
  484.   
  485.   strcpy( InItemTextBuf, "_________________________" );
  486.   strcpy( OutItemTextBuf, InItemTextBuf );
  487.   InItem.text  = &InItemTextBuf[0];
  488.   OutItem.text = &OutItemTextBuf[0];
  489.   
  490.   CatMenu[0].item  = Cat1List;
  491.   CatMenu[1].item  = Cat2List;
  492.   UtMenu.item  = UtList;
  493.   InpMenu.item  = &InItem;
  494.   OutMenu.item  = &OutItem;
  495.  
  496.     c_cat_array[ORGANISM] = &c_organism[0];
  497.   c_cat_array[TYPE_ORG] = &c_type_org[0];
  498.   c_cat_array[DISEASE]  = &c_disease[0];
  499.   c_cat_array[SIDE_EFF] = &c_side_eff[0];
  500.   c_cat_array[M_ADMIN]  = &c_m_admin[0];
  501.   c_cat_array[DRUG1]  = &c_drug[0];
  502.   c_cat_array[DRUG2]  = &c_drug[0];
  503.  
  504.   a_cat_array[ORGANISM] = &a_organism[0];
  505.   a_cat_array[TYPE_ORG] = &a_type_org[0];
  506.   a_cat_array[DISEASE]  = &a_disease[0];
  507.   a_cat_array[SIDE_EFF] = &a_side_eff[0];
  508.   a_cat_array[M_ADMIN]  = &a_m_admin[0];
  509.   a_cat_array[DRUG1]  = &a_drug[0];
  510.   a_cat_array[DRUG2]  = &a_drug[0];
  511.  
  512.   /* Initialize menu text */
  513.   for ( wy = 0; wy < 2; wy++ ) {
  514.     for ( wx = 0, gmip = CatMenu[wy].item; wx < CatMenu[wy].num_items;
  515.     wx++, gmip++ ) {
  516.       curr_index[gmip->code] = 1;
  517.       gmip->text = c_cat_array[gmip->code][1];
  518.     }
  519.   }
  520.   curr_mode = 0;
  521.   abbreviate( -1 );
  522.   
  523.   /* Get screen parameters */
  524.   ug_gparms( &xsize, &ysize, &ncolor, &chrx, &chry);
  525.   
  526.   if ( ncolor < 8 ) {
  527.     gm_intcolor = 1;
  528.     gm_txtcolor = 0;
  529.     gm_outcolor = 0;
  530.   } else {
  531.     gm_intcolor = 7;
  532.     gm_txtcolor = 4;
  533.     gm_outcolor = 0;
  534.   }
  535.  
  536.   /* Initialize menus and set window positions */
  537.   InitGMenu( &InpMenu, chrx, chry );
  538.   InitGMenu( &OutMenu, chrx, chry );
  539.   InitGMenu( &UtMenu, chrx, chry );
  540.   InitGMenu( &CatMenu[0], chrx, chry );
  541.   InitGMenu( &CatMenu[1], chrx, chry );
  542.   x_inp = (xsize - InpMenu.x1) / 2;
  543.   x_out = (xsize - OutMenu.x1) / 2;
  544.   x_ut = (xsize - UtMenu.x1) / 2;
  545.   if ( (wx = CatMenu[0].x1 - CatMenu[1].x1) >= 0 ) {
  546.     x_cat = (xsize - CatMenu[0].x1) / 2;
  547.     x_cat1 = x_cat + CatMenu[0].x1 + 2;
  548.     CatMenu[0].x0 = 0;
  549.     CatMenu[1].x0 = wx / 2;
  550.   } else {
  551.     x_cat = (xsize - CatMenu[1].x1) / 2;
  552.     x_cat1 = x_cat + CatMenu[1].x1 + 2;
  553.     CatMenu[0].x0 = - (wx / 2);
  554.     CatMenu[1].x0 = 0;
  555.   }
  556.   y_cat = ysize / 4;
  557.   y_ut = y_cat + 30;
  558.   y_out = y_ut + 30;
  559.   y_inp = y_out + 30;
  560.  
  561.   InpMenu.x0 = 0; /* Menu position relative to window */
  562.   InpMenu.y0 = 0;
  563.   OutMenu.x0 = 0; /* Menu position relative to window */
  564.   OutMenu.y0 = 0;
  565.   UtMenu.x0 = 0;  /* Menu position relative to window */
  566.   UtMenu.y0 = 0;
  567.   CatMenu[0].y0 = CatMenu[1].y0 = 0;
  568.  
  569.   InitFlag = 1;
  570.     }
  571.  
  572.     IORTNCDE = 0;       /* good return for data */
  573.     switch( IOREQCDE ) {
  574.     case RQ_ATTENTION:
  575.   /* This is invoked at the request of the user from the
  576.      execute menu.  It allows parameters to be changed or
  577.      altered.  Any graphics or other interactions are allowable
  578.      as usual for the other options.
  579.   */
  580.   asc_fp = (FILE * ) 0;
  581.   iv_num = 1;
  582.   PutStr("Create <.nna> file ? - previous bsbwh.nna will be deleted.\n");
  583.   PutStr( "Press right button to abort, left button to continue.\n" );
  584.   for ( ; ; ) {
  585.     ug_mouse( &key, &xp, &yp, &button );
  586.     if ( button == MBUT_RIGHT ) goto end_attn;
  587.     if ( button == MBUT_LEFT  ) break;
  588.   }
  589. #if !defined(MAC) && !defined(VMS)
  590.   unlink( "bsbwh.nna" );  /* Delete file if it exists */
  591. #endif
  592.   asc_fp = fopen( "bsbwh.nna", "w" ); /* Open for writing */
  593.   if ( asc_fp == (FILE *)0 ) goto end_attn;
  594.   fprintf( asc_fp,
  595.     "! Input file for antibiotic example. Autoassociative network%s",
  596.      NEW_LINE_STR );
  597.   
  598.   /* Display menus and await inputs */
  599.   ug_window( InpMenu.key, gm_intcolor, x_inp, y_inp,
  600.        x_inp + InpMenu.x1 + 2, y_inp + InpMenu.y1 + 2);
  601.   ug_window( UtMenu.key, gm_intcolor, x_ut, y_ut,
  602.        x_ut + UtMenu.x1 + 2, y_ut + UtMenu.y1 + 2);
  603.   ug_window( CatMenu[curr_mode].key, gm_intcolor, x_cat, y_cat,
  604.        x_cat1, y_cat + CatMenu[0].y1 + 2);
  605.  
  606.   DispGMenu( &InpMenu );
  607.   DispGMenu( &UtMenu );
  608.   DispGMenu( &CatMenu[curr_mode] );
  609.   
  610.   for ( ; ; ) {
  611.     while ( (gmip=LookGMenu(&CatMenu[curr_mode],&button))
  612.       != (GMENU_ITEM *) 0 ) {
  613.         if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  614.         ci = &curr_index[gmip->code];
  615.         nw = num_words[gmip->code];
  616.         if (button == MBUT_LEFT ) *ci = (*ci + 1) % nw;
  617.         else *ci = (*ci + nw - 1) % nw;
  618.         gmip->text = c_cat_array[gmip->code][*ci];
  619.         DispGItem( &CatMenu[curr_mode], gmip,
  620.        gm_intcolor, gm_txtcolor );
  621.         abbreviate( -1 );
  622.         DispGMenu( &InpMenu );
  623.       }
  624.       /* If button was pressed, wait for it to be released */
  625.       while ( button != 0 )
  626.         ug_mouse( &key, &xp, &yp, &button );
  627.     }
  628.   
  629.     while ( (gmip=LookGMenu( &UtMenu, &button )) != (GMENU_ITEM *) 0 ) {
  630.       switch( gmip->code ) {
  631.     case M_MODE:
  632.         if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  633.       curr_mode = (curr_mode + 1) % 2;
  634.       gmip->text = mode_text[curr_mode];
  635.       DispGItem( &UtMenu, gmip, gm_intcolor, gm_txtcolor );
  636.       ug_winclr( CatMenu[0].key );
  637.       DispGMenu( &CatMenu[curr_mode] );
  638.       abbreviate( -1 );
  639.       DispGMenu( &InpMenu );
  640.     }
  641.     break;
  642.     case M_CLEAR:
  643.         if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  644.     /* Set current index to 0 for all menu items and redisplay */
  645.       for ( gmip = CatMenu[curr_mode].item, wx = 0;
  646.       wx < CatMenu[curr_mode].num_items; wx++, gmip++ ) {
  647.         curr_index[gmip->code] = 0;
  648.         gmip->text = c_cat_array[gmip->code][0];
  649.       }
  650.       DispGMenu( &CatMenu[curr_mode] );
  651.       abbreviate( -1 );
  652.       DispGMenu( &InpMenu );
  653.     }
  654.     break;
  655.     case M_ENTER:
  656.         if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  657.       fprintf( asc_fp, "! F%ld: ", (long)iv_num );
  658.       iv_num++;
  659.       fprintf( asc_fp, InItem.text );
  660.       fprintf( asc_fp, "%s ", NEW_LINE_STR );
  661.       for ( sp = InItem.text, wx = 0; wx < 25; sp++, wx++ ) {
  662.         encode( (int)(*sp), wr );
  663.         if ( wx != 0 && wx%3 == 0 ) fprintf( asc_fp, "%s&", NEW_LINE_STR);
  664.         for ( wy = 0; wy < 6; wy++ )
  665.           fprintf( asc_fp, " %2.0f.", wr[wy] );
  666.       }
  667.       fprintf( asc_fp, NEW_LINE_STR );
  668.     }
  669.     break;
  670.     case M_QUIT:
  671.         if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  672.       IORTNCDE = -1;
  673.       goto end_attn;
  674.     }
  675.     break;
  676.         }
  677.       /* If button was pressed, wait for it to be released */
  678.       while ( button != 0 )
  679.         ug_mouse( &key, &xp, &yp, &button );
  680.     }
  681.   }
  682.  
  683. end_attn:     
  684.   if ( asc_fp != (FILE * ) 0 ) fclose( asc_fp );
  685.   break;
  686.  
  687.     case RQ_LSTART:       /* starting learn */
  688.   /* This tells the user that the program is about to start
  689.      learning.  It is called once for a LEARN ALL, LEARN ONE,
  690.      LEARN N, or LEARN START
  691.   */
  692.   break;
  693.  
  694.     case RQ_LEARNIN:        /* read training input */
  695.   /* IODATA points to an empty array of IOCOUNT elements.  The
  696.      values placed in this array by the user will become the
  697.      inputs to the network for training purposes.
  698.   */
  699.  
  700.     case RQ_READ:       /* read test data */
  701.   /* IODATA points to an empty array of IOCOUNT values.  The
  702.      user must fill in these values.  The elements of the
  703.      array will become the "sum" of the inputs to the input
  704.      layer of processing elements.
  705.   */ 
  706.  
  707.   wr_count = 0;
  708.  
  709.   ug_window( InpMenu.key, gm_intcolor, x_inp, y_inp,
  710.        x_inp + InpMenu.x1 + 2, y_inp + InpMenu.y1 + 2);
  711.   ug_window( UtMenu.key, gm_intcolor, x_ut, y_ut,
  712.        x_ut + UtMenu.x1 + 2, y_ut + UtMenu.y1 + 2);
  713.   ug_window( CatMenu[curr_mode].key, gm_intcolor, x_cat, y_cat,
  714.        x_cat1, y_cat + CatMenu[0].y1 + 2);
  715.  
  716.   DispGMenu( &InpMenu );
  717.   DispGMenu( &UtMenu );
  718.   DispGMenu( &CatMenu[curr_mode] );
  719.  
  720.   for ( ; ; ) {
  721.     while ( (gmip=LookGMenu(&CatMenu[curr_mode],&button))
  722.       != (GMENU_ITEM *) 0 ) {
  723.         if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  724.         ci = &curr_index[gmip->code];
  725.         nw = num_words[gmip->code];
  726.         if (button == MBUT_LEFT ) 
  727.       *ci = (*ci + 1) % nw;
  728.         else 
  729.       *ci = (*ci + nw - 1) % nw;
  730.         gmip->text = c_cat_array[gmip->code][*ci];
  731.         DispGItem( &CatMenu[curr_mode], gmip,
  732.        gm_intcolor, gm_txtcolor );
  733.         abbreviate( gmip->code );
  734.         DispGMenu( &InpMenu );
  735.       }
  736.       /* If button was pressed, wait for it to be released */
  737.       while ( button != 0 )
  738.         ug_mouse( &key, &xp, &yp, &button );
  739.     }
  740.   
  741.     while ( (gmip=LookGMenu( &UtMenu, &button )) != (GMENU_ITEM *) 0 ) {
  742.       switch( gmip->code ) {
  743.       case M_MODE:
  744.         if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  745.     curr_mode = (curr_mode + 1) % 2;
  746.     gmip->text = mode_text[curr_mode];
  747.     DispGItem( &UtMenu, gmip, gm_intcolor, gm_txtcolor );
  748.     ug_winclr( CatMenu[0].key );
  749.     DispGMenu( &CatMenu[curr_mode] );
  750.     if ( IOREQCDE == RQ_LEARNIN ) abbreviate( -1 );
  751.     DispGMenu( &InpMenu );
  752.         }
  753.         break;
  754.       case M_CLEAR:
  755.         if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  756.     /* Set current index to 0 for all menu items and redisplay */
  757.     for ( gmip = CatMenu[curr_mode].item, wx = 0;
  758.          wx < CatMenu[curr_mode].num_items; wx++, gmip++ ) {
  759.       curr_index[gmip->code] = 0;
  760.       gmip->text = c_cat_array[gmip->code][0];
  761.     }
  762.     DispGMenu( &CatMenu[curr_mode] );
  763.     abbreviate( -1 );
  764.     DispGMenu( &InpMenu );
  765.         }
  766.         break;
  767.       case M_ENTER:
  768.         if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  769.     /* Fill in IODATA with bit patterns for current input */
  770.     for ( sp = InItem.text, wx = 0, p_outv = IODATA;
  771.          wx < 25; sp++, wx++, p_outv += 6 ) {
  772.       encode( (int)(*sp), p_outv );
  773.     }
  774.     goto end_read;
  775.         }
  776.         break;
  777.       case M_QUIT:
  778.         if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  779.     IORTNCDE = -1;
  780.     goto end_read;
  781.         }
  782.         break;
  783.       }
  784.       /* If button was pressed, wait for it to be released */
  785.       while ( button != 0 )
  786.         ug_mouse( &key, &xp, &yp, &button );
  787.     }
  788.   }
  789. end_read:
  790.   break;
  791.  
  792.     case RQ_LEARNOUT:       /* read desired output */
  793.   /* IODATA points to an empty array of IOCOUNT values.  The
  794.      elements of the array will become the desired outputs for
  795.      training purposes.  These desired outputs correspond to
  796.      the most recent "RQ_LEARNIN" request.
  797.   */
  798.  
  799.   break;
  800.  
  801.     case RQ_WRITE:        /* write out results */
  802.   /* IODATA points to an array of IOCOUNT "float" type values.
  803.      The values are the outputs of the top-most layer of the
  804.      network.
  805.   */
  806.   IORTNCDE = 1; /* Redraw */
  807.   break;
  808.  
  809.     case RQ_LEARNRSLT:
  810.   /* IODATA points to an array of IOCOUNT values.  These are the
  811.      output of the network caused by the inputs from RQ_LEARNIN.
  812.   */
  813.  
  814.   ug_window( OutMenu.key, gm_intcolor, x_out, y_out,
  815.        x_out + OutMenu.x1 + 2, y_out + OutMenu.y1 + 2);
  816.  
  817.   for ( sp = OutItem.text, wx = 0, p_outv = IODATA;
  818.       wx < 25; wx++, p_outv += 6 )
  819.     *sp++ = decode( p_outv, 0.1 );
  820.  
  821.   DispGMenu( &OutMenu );
  822.  
  823.   /* Lock Items */
  824.   for ( wy = 0; wy < 2; wy++ )
  825.     for ( wx = 0, gmip = CatMenu[wy].item;
  826.     wx < CatMenu[wy].num_items; wx++, gmip++ )
  827.     gmip->flag |= GM_LOCKED;
  828.   
  829.   UtList[M_MODE].flag |= GM_LOCKED;
  830.   UtList[M_CLEAR].flag |= GM_LOCKED;
  831.   UtList[M_QUIT].flag |= GM_LOCKED;
  832.  
  833.   for ( ; ; ) {
  834.     while ( (gmip=LookGMenu( &UtMenu, &button )) == (GMENU_ITEM *)0 ) ;
  835.     if ( button == MBUT_LEFT || button == MBUT_RIGHT ) break;
  836.   }
  837.   /* Unlock Items */
  838.   for ( wy = 0; wy < 2; wy++ )
  839.     for ( wx = 0, gmip = CatMenu[wy].item;
  840.     wx < CatMenu[wy].num_items; wx++, gmip++ )
  841.     gmip->flag &= ~GM_LOCKED;
  842.   
  843.   UtList[M_MODE].flag &= ~GM_LOCKED;
  844.   UtList[M_CLEAR].flag &= ~GM_LOCKED;
  845.   UtList[M_QUIT].flag &= ~GM_LOCKED;
  846.  
  847.   IORTNCDE = 1;
  848.   break;
  849.  
  850.     case RQ_WRSTEP:       /* write interim results */
  851.   /* each recall cycle for the Hopfield and BAM control strategies,
  852.      the intermediate output is made available to userio to test
  853.      for convergence or other desired states.  This option is
  854.      not used for other control strategies.
  855.   */
  856.  
  857.   if ( wr_count++ % 5 == 0 ) {
  858.     ug_window( OutMenu.key, gm_intcolor, x_out, y_out,
  859.          x_out + OutMenu.x1 + 2, y_out + OutMenu.y1 + 2);
  860.  
  861.     for ( sp = OutItem.text, wx = 0, p_outv = IODATA;
  862.     wx < 25; wx++, p_outv += 6 )
  863.       *sp++ = decode( p_outv, 0.1 );
  864.  
  865.     DispGMenu( &OutMenu );
  866.  
  867.     /* Lock Items */
  868.     for ( wy = 0; wy < 2; wy++ )
  869.       for ( wx = 0, gmip = CatMenu[wy].item;
  870.       wx < CatMenu[wy].num_items; wx++, gmip++ )
  871.         gmip->flag |= GM_LOCKED;
  872.   
  873.     UtList[M_MODE].flag |= GM_LOCKED;
  874.     UtList[M_CLEAR].flag |= GM_LOCKED;
  875.  
  876.     sprintf( buf, "Recall iteration number %ld%s", (long)wr_count, NEW_LINE_STR );
  877.     PutStr( buf );
  878.     PutStr( "Select ENTER to continue, QUIT to terminate recall\n" );
  879.     for ( ; ; ) {
  880.       while ( (gmip=LookGMenu(&UtMenu,&button )) == (GMENU_ITEM *)0 ) ;
  881.       switch( gmip->code ) {
  882.       case M_ENTER:
  883.         if ( button == MBUT_LEFT || button == MBUT_RIGHT ) goto end_wr;
  884.         break;
  885.       case M_QUIT:
  886.         if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  887.     IORTNCDE = -1;
  888.     goto end_wr;
  889.         }
  890.         break;
  891.       }
  892.     }
  893. end_wr:
  894.     /* Unlock Items */
  895.     for ( wy = 0; wy < 2; wy++ )
  896.       for ( wx = 0, gmip = CatMenu[wy].item;
  897.       wx < CatMenu[wy].num_items; wx++, gmip++ )
  898.       gmip->flag &= ~GM_LOCKED;
  899.   
  900.     UtList[M_MODE].flag &= ~GM_LOCKED;
  901.     UtList[M_CLEAR].flag &= ~GM_LOCKED;
  902.   }
  903.  
  904.   break;
  905.  
  906.     case RQ_RSTART:       /* starting recall */
  907.   /* This tells the user that the program is about to start
  908.      a recall.  It is called once for a REACLL ALL, RECALL ONE,
  909.      RECALL N, or RECALL START.
  910.   */
  911.   break;
  912.   
  913.     case RQ_RCLTST:   /* read desired output during recall test */
  914.   /* IODATA points to an empty array of IOCOUNT values.  The
  915.      elements of the array will become the desired outputs for
  916.      recall purposes.  This request is only made during a
  917.      Execute Network/Recall Test.
  918.   */
  919.  
  920.   break;
  921.  
  922.     case RQ_TERM:       /* terminate interface */
  923.   /* close any files which may be open.  Deallocate any memory
  924.      which was allocated.  (This is VERY VERY important.  If
  925.      allocated memory is NOT released, dos memory will become
  926.      fragmented and it will become necessary to reboot.
  927.   */
  928.  
  929.   PutStr( "bye bye\n" );
  930.   break;
  931.     }
  932.  
  933.     return;
  934. }
  935.